<?php 
        require("connection_mysql.inc.php");
        
        $tempsLastUpdate = mysql_fetch_array(mysql_query("SELECT MIN(derniereSauvegarde) FROM sauvegarde"));
        if ($tempsLastUpdate)
                $tempsLastUpdate = $tempsLastUpdate['derniereSauvegarde'];
        else 
                $tempsLastUpdate = 0;
        $tempsActuel = time();
        
        if ($tempsActuel - $tempsLastUpdate > (3 * 60*60))
        {
                $url = 'http://ade52-savoie.grenet.fr/ade/webapi?function=connect&login=ETUDIANT&password=etsa';
                $agent = '';
                $id = '';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_USERAGENT, $agent);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 8);   
                curl_setopt($ch, CURLOPT_COOKIE, $id);
         
                $ret = curl_exec($ch);
                if (curl_errno($ch))
                {
                        return false;
                }
                curl_close($ch);
                if (!(strpos($ret, '<session') === false))
                {
                        $page = new DOMDocument();
                        $page->loadXML($ret);
                        
                        $session = $page->getElementsByTagName("session")->item(0)->attributes->item(0)->value;
                        $url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=setProject&projectId=4";
                        
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_TIMEOUT, 8);   
                        curl_setopt($ch, CURLOPT_COOKIE, $id);
                 
                        $ret = curl_exec($ch);
                        if (curl_errno($ch))
                        {
                                print curl_error($ch); 
                                return false;
                        }
                        curl_close($ch);
                        
                        $week = date('W') - 32;
                        if ($week <= 0) $week += 52;
                        mysql_query("DELETE from sauvegarde where semaine < $week");
                        
                        $requete = "SELECT distinct(section.id) FROM section left join sauvegarde on sauvegarde.id = section.id ORDER BY derniereSauvegarde ASC LIMIT 0,3";
                        $donnees = mysql_query($requete);
                        //echo mysql_num_rows($donnees);
                        $ids = array();
                        $k = 0;
                        
                        while ($result = mysql_fetch_array($donnees))
                        {
                                $ids[$k] = $result['id'];
                                $k ++;
                                
                        }
                        /*if ($k == 0){
                                $requete = "SELECT section.id FROM section ";
                                $donnees = mysql_query($requete);
                                $ids = array();
                                $k = 0;
                                
                                while ($result = mysql_fetch_array($donnees))
                                {
                                        $ids[$k] = $result['id'];
                                        $k ++;
                                }
                                //on affecte $weekMax pour ne pas surcharger le serveur sur la premiere sauvegarde
                                $weekMax = $week + 1;
                        }*/
                        //else {
                                //on affecte $weekMax pour ne pas surcharger le serveur sur la premiere sauvegarde
                                $weekMax = $week + 2;
                        //}
                        
                        foreach ($ids as $id)
                        {
                                
                                for ($j = $week;  $j < $weekMax; $j++)
                                {
                                        for ($i = 0; $i<5; $i++)
                                        {
                                                $url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=getEvents&resources=$id&detail=0&weeks=$j&days=$i"; //getEvents&resources=1165&detail=4&weeks=12&days=0
                                                echo "<br/>$url";
                                                $ch = curl_init();
                                                curl_setopt($ch, CURLOPT_USERAGENT, $agent);
                                                curl_setopt($ch, CURLOPT_URL, $url);
                                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                                curl_setopt($ch, CURLOPT_TIMEOUT, 8); 
                                                curl_setopt($ch, CURLOPT_COOKIE, $id);
                                         
                                                $ret = curl_exec($ch);
                                                if (curl_errno($ch))
                                                {
                                                        print curl_error($ch);
                                                        echo "ERREUR !!!!!!";
                                                        return false;
                                                }
                                                curl_close($ch);
                                                //echo "section : $id, semaine : $week,  jour : $i"; 
                                                $donnees = mysql_query("SELECT * from sauvegarde WHERE id = $id AND semaine = $j AND jour = $i;" );
                                                if(mysql_fetch_array($donnees))
                                                {
                                                        mysql_query("UPDATE sauvegarde  set derniereSauvegarde = $tempsActuel, xml = '$ret' WHERE id = $id AND semaine = $j AND jour = $i;" );
                                                        //echo $result['id']." test<br/>";
                                                } 
                                                else
                                                {
                                                        mysql_query("INSERT into sauvegarde (id, derniereSauvegarde, semaine, jour, xml) values ($id, $tempsActuel , $j, $i, '$ret');");
                                                        //echo $result['id']." test2<br/>";
                                                }
                                        }
                                }
                        }
                }
        }
?>